package com.android.util;


import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesUtils {


    private SharedPreferences mSharedPreferences;
    private static SharedPreferencesUtils mSharedPreferencesUtils;
    private Context context;

    private SharedPreferencesUtils(Context context) {
        mSharedPreferences = context.getSharedPreferences("sharePreferences", Context.MODE_PRIVATE);
    }

    public static SharedPreferencesUtils getInit(Context context) {
        if (mSharedPreferencesUtils == null) {
            synchronized (SharedPreferencesUtils.class) {
                if (mSharedPreferencesUtils == null) {
                    mSharedPreferencesUtils = new SharedPreferencesUtils(context);
                }
            }
        }
        return mSharedPreferencesUtils;
    }

    public synchronized void putString(String key, String value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, value);
        boolean isOk = editor.commit();
        LogUtils.i("saveSharePreferences" + isOk);
    }

    public synchronized String getString(String key) {
        return mSharedPreferences.getString(key, "");
    }

    /**
     * 写字符串
     *
     * @param context
     * @param key
     * @param value
     * @return
     */
    public static boolean putString(Context context, String key, String value) {
        SharedPreferences settings = SharedPreferencesUtils.getInit(context).mSharedPreferences;
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    /**
     * 获取字符串
     *
     * @param context
     * @param key
     * @return
     */
    public static String getString(Context context, String key) {
        SharedPreferences settings = SharedPreferencesUtils.getInit(context).mSharedPreferences;
        return settings.getString(key, "");
    }

    /**
     * 写字符串
     *
     * @param context
     * @param key
     * @param value
     * @return
     */
    public static boolean putInt(Context context, String key, int value) {
        SharedPreferences settings = SharedPreferencesUtils.getInit(context).mSharedPreferences;
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    /**
     * 获取字符串
     *
     * @param context
     * @param key
     * @return
     */
    public static int getInt(Context context, String key) {
        SharedPreferences settings = SharedPreferencesUtils.getInit(context).mSharedPreferences;
        return settings.getInt(key, 0);
    }

    /**
     * put boolean preferences
     *
     * @param context
     * @param key     The name of the preference to modify
     * @param value   The new showapi_res_body for the preference
     * @return True if the new values were successfully written to persistent storage.
     */
    public static boolean putBoolean(Context context, String key, boolean value) {
        SharedPreferences settings = SharedPreferencesUtils.getInit(context).mSharedPreferences;
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    /**
     * get boolean preferences, default is false
     *
     * @param context
     * @param key     The name of the preference to retrieve
     * @return The preference showapi_res_body if it exists, or false. Throws ClassCastException if there is a preference with this
     * name that is not a boolean
     * @see #getBoolean(Context, String, boolean)
     */
    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

    /**
     * get boolean preferences
     *
     * @param context
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference showapi_res_body if it exists, or defValue. Throws ClassCastException if there is a preference with
     * this name that is not a boolean
     */
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences settings = SharedPreferencesUtils.getInit(context).mSharedPreferences;
        return settings.getBoolean(key, defaultValue);
    }
//    public  static void SaveBean(String key, Object object) {
//     String content="";
//        content= JsonUtils.toJson(object);
//        try {
//
//            SharedPreferences.Editor editor = mSharedPreferencesUtils.mSharedPreferences.edit();
//            editor.putString(key, content);
//
//            editor.commit();
//        } catch (Exception e) {
//            // TODO Auto-generated
//        }
//
//    }

}
